﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VoteUphold;
using System.Net;
using Provider;
using System.Threading;
using System.Net.Sockets;
using PasswordEnumerator;

namespace Vote
{
    public partial class GetProxyTest : Form
    {
        public GetProxyTest()
        {
            InitializeComponent();
        }
        //数
        /// <summary>
        /// 数
        /// </summary>
        private VoteCount voteCount;
        private Random rd = new Random();
        private Email email;
        private void GetProxyTest_Load(object sender, EventArgs e)
        {
            voteCount = new VoteCount();
            //email = new Email();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            email = new Email();
            email.Send("cn", "接到了没"+rd.Next(9999));
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            
           Thread th = new Thread(new ThreadStart(PrintIp2));
            th.Start();
        }

        private void PrintIp2()
        {
            List<string> hostPortList = GetProxy.GetProxyProxycn();
            for (int i = 0; i < hostPortList.Count; i++)
            {
                try
                {
                    string hostPort = hostPortList[i];
                    string[] aa = hostPort.Split(':');
                    IWebProxy iweb;
                    if (aa != null && aa.Length >= 2)
                    {
                        iweb = new WebProxy(aa[0], Convert.ToInt32(aa[1]));
                    }
                    else
                    {
                        continue;
                    }
                    WebClient wc = new WebClient();
                    wc.Proxy = iweb;
                    string voteResult = wc.DownloadString("http://www.net.cn/static/customercare/yourIP.asp");
                    MatchData md = new MatchData("您的本地上网IP是：<h2>", "</h2>", voteResult);
                    md.Match(); md.Substring();
                    if (md.Result == "")
                    {
                        this.SetText(voteResult);
                    }
                    else
                    {
                        this.SetText(md.Result);
                    }
                   
                }
                catch (Exception ex)
                {
                    this.SetText(ex.Message);
                }
            }
        }

        /// <summary>
        /// 1111111
        /// </summary>
        private void PrintIp()
        {
            List<string> hostPortList = GetProxy.GetProxyProxycn();
            string sendData= GetSendData();
            for (int i = 0; i < hostPortList.Count; i++)
            {
                try
                {
                    string hostPort = hostPortList[i];
                    string[] aa = hostPort.Split(':');
                    IPEndPoint ipendPoint = null;
                    if (aa != null && aa.Length >= 2)
                    {
                        ipendPoint = new IPEndPoint(IPAddress.Parse(aa[0]), Convert.ToInt32(aa[1]));
                    }
                    else
                    {
                        continue;
                    }
                    Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建Socket
                    //c.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 3000);
                    //c.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 3000);

                    c.Connect(ipendPoint);//连接到服务器

                    ///向服务器发送信息
                    byte[] bs = Encoding.Default.GetBytes(sendData);//把字符串编码为字节
                    c.Send(bs, bs.Length, 0);//发送信息
                    ///接受从服务器返回的信息
                    StringBuilder recvStr = new StringBuilder();
                    byte[] recvBytes = new byte[1024];
                    int bytes;
                    do
                    {
                        bytes = c.Receive(recvBytes, recvBytes.Length, 0);//从服务器端接受返回信息
                        recvStr.Append(Encoding.Default.GetString(recvBytes, 0, bytes));
                    } while (bytes > 0);
                    //禁用上次的发送和接受 
                    c.Shutdown(SocketShutdown.Both);
                    ///一定记着用完socket后要关闭
                    c.Close();
                    string voteResult = recvStr.ToString();
                   MatchData md= new MatchData("您的本地上网IP是：<h2>", "</h2>", voteResult);
                   md.Match(); md.Substring();
                   this.SetText(md.Result);

                }
                catch (Exception ex)
                {
                    hostPortList.Remove(hostPortList[i]);
                    this.SetText(ex.Message);
                }

            }
        }

        public string GetSendData()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("GET /static/customercare/yourIP.asp HTTP/1.1" + Environment.NewLine);
            sb.Append("Host: www.net.cn" + Environment.NewLine);
            //sb.Append("Accept: image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*" + Environment.NewLine);
            sb.Append("Accept-Language: zh-CN" + Environment.NewLine);
            sb.Append("User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.2)" + Environment.NewLine);
            sb.Append("Accept-Encoding: gzip, deflate" + Environment.NewLine);
            sb.Append("Connection: Close" + Environment.NewLine);
            
            sb.Append(Environment.NewLine);
            return sb.ToString();
        }


        // 给值
        /// <summary>
        ///  给值
        /// </summary>
        /// <param name="text"></param>
        private void SetText(string text)
        {
            if (this.rtbResult.InvokeRequired)
            {
                SetTextCallback set = new SetTextCallback(SetText);
                this.Invoke(set, text);
            }
            else
            {
                this.rtbResult.AppendText(string.Format("{0}.{1} {2}{3}", voteCount.NumberCount++, DateTime.Now, text, Environment.NewLine));
                this.rtbResult.ScrollToCaret();
                if (voteCount.NumberCount % 200 == 0)
                {
                    Log();
                }
            }
        }

        //日志
        /// <summary>
        /// 日志
        /// </summary>
        private void Log()
        {
            if (this.rtbResult.InvokeRequired)
            {
                LogCallback set = new LogCallback(Log);
                this.Invoke(set);
            }
            else
            {
                //email.Send("111", "112121212");
                //email.Send("22222222222", "12121212121212");
                VoteLog.AddResult("result.fei", this.rtbResult.Text);
                this.rtbResult.Clear();
            }
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            Log();
        }

      

    }
}

